//
//  RequestTccInfo.m
//  evidenceGather
//
//  Created by 王继伟 on 13-9-1.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "RequestTccInfo.h"
#import "TccSiteServicesSvc.h"
#import "ProviceVO.h"
#import "ProjectVO.h"
#import "ProblemClassVO.h"

@implementation RequestTccInfo

@synthesize serverInfo;


//用户验证
-(int) loginValidate:(NSString *) userName pwd:(NSString *)pwd{
    
    int validate=0;
    
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    
    TccSiteServicesSoapBindingResponse *response;
    
    TccSiteServicesSvc_LoginValidate *request=[[TccSiteServicesSvc_LoginValidate alloc] init];
    request.account = userName;
    request.password = pwd;
   
    response=[binding LoginValidateUsingParameters:request];
    
//    NSLog(@"response %@",[[response error] localizedDescription] );
   
    NSArray *responseBodyParts=response.bodyParts;
    
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine
        
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return 0;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return 0;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_LoginValidateResponse class]]) {
        TccSiteServicesSvc_LoginValidateResponse* response = bodyPart;
        
        validate= response.LoginValidateResult.intValue;
        
    }
    return validate;
}

//省份查询接口
-(NSArray *) getProvinceInfo:(NSString *) userName pwd:(NSString *)pwd{
    NSMutableArray * provinceVOs = nil;
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    TccSiteServicesSoapBindingResponse *response;
    TccSiteServicesSvc_GetProvinceInfo *request=[[TccSiteServicesSvc_GetProvinceInfo alloc] init];
    request.username=userName;
    request.password=pwd;
    response=[binding GetProvinceInfoUsingParameters:request];
    NSArray *responseBodyParts=response.bodyParts;
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine        
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return provinceVOs;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return provinceVOs;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_GetProvinceInfoResponse class]]) {
        TccSiteServicesSvc_GetProvinceInfoResponse* response = bodyPart;
//        NSLog(@"%@",response.GetProvinceInfoResult);
        NSString * result=response.GetProvinceInfoResult;
        result=[result stringByReplacingOccurrencesOfString:@"<provinceClassInfo><provinceInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</provinceInfo></provinceClassInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</provinceInfo><provinceInfo>" withString:@""];
        
        result=[result stringByReplacingOccurrencesOfString:@"</prvcode><prvname>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</prvname><prvcode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"<prvcode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</prvname>" withString:@","];
        
//        NSLog(@" after replace%@",result);;
        
        NSArray *values=[result componentsSeparatedByString:@","];
        int count=[values count];
        if(count>0){
            provinceVOs =[[NSMutableArray alloc] init];
            for(int i=1;i<count-1; ){
                
                ProviceVO *proviceVO= [[ProviceVO alloc] init];
                proviceVO.prvcode=[values objectAtIndex:i];
                proviceVO.prvname=[values objectAtIndex:(i+1)];
                proviceVO.loginuser=userName;
                [provinceVOs addObject:proviceVO];
                i = i+2;
            }
        }
    }
//    for (int i = 0; i < provinceVOs.count; i++) {
//        NSLog(@"prvcode : %@,prvname: %@",[[provinceVOs objectAtIndex:i] prvcode],[[provinceVOs objectAtIndex:i] prvname]);
//    }
    return provinceVOs;
    
}

//指定省份项目查询接口
-(NSArray *)GetProjectInfoByProvince:(NSString *) userName pwd:(NSString *)pwd projcode :(NSString*)projcode provcode :(NSString*)provcode{
    NSMutableArray * projectVOs;
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    TccSiteServicesSoapBindingResponse *response;
    TccSiteServicesSvc_GetProjectInfoByProvince *request=[[TccSiteServicesSvc_GetProjectInfoByProvince alloc] init];
    request.username=userName;
    request.password=pwd;
    request.prv_code=provcode;
//    request.username=@"zyk";
//    request.password=@"goon";
//    request.prv_code=@"01";
    if(nil == projcode)
    {
        request.proj_code = @"";
    }else{
        request.proj_code=projcode;
    }
    
    response=[binding GetProjectInfoByProvinceUsingParameters:request];
    
    NSArray *responseBodyParts=response.bodyParts;
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine
        for (id bodypat_t in responseBodyParts) {
            TccSiteServicesSvc_GetProjectInfoByProvinceResponse * rp = bodypat_t;
//             NSLog(@"%@",rp.GetProjectInfoByProvinceResult);
        }
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return projectVOs;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return projectVOs;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_GetProjectInfoByProvinceResponse class]])
    {
        TccSiteServicesSvc_GetProjectInfoByProvinceResponse * response = bodyPart;
//        NSLog(@"%@",response.GetProjectInfoByProvinceResult);
        
        NSString * result=response.GetProjectInfoByProvinceResult;
        result=[result stringByReplacingOccurrencesOfString:@"<ProvProjectInfo><projectInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</projectInfo></ProvProjectInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</projectInfo><projectInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</status><projcode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"<projcode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</projcode><projname>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</projname><status>" withString:@","];
        
        
        
        result=[result stringByReplacingOccurrencesOfString:@"</status>" withString:@","];
        
//        NSLog(@" after replace%@",result);
        
        NSArray *values=[result componentsSeparatedByString:@","];
        int count=[values count];
        if(count>0){
            projectVOs =[[NSMutableArray alloc] init];
            for(int i=1;i<count-2; ){
                ProjectVO *projectVO= [[ProjectVO alloc] init];
                projectVO.projcode=[values objectAtIndex:i];
                projectVO.projname=[values objectAtIndex:(i+1)];
                projectVO.status = [values objectAtIndex:(i+2)];
                projectVO.prvcode=provcode;
                projectVO.loginuser=userName;
                [projectVOs addObject:projectVO];
                i=i+3;
            }
        }
    }
    return projectVOs;
    
}

//问题类别查询接口
-(NSArray *) getQuestTypeWithUser:(NSString *)loginuser
{
    NSMutableArray * probClsVOs = nil;
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    TccSiteServicesSoapBindingResponse *response;
    TccSiteServicesSvc_GetQuestionType *request=[[TccSiteServicesSvc_GetQuestionType alloc] init];
    response=[binding GetQuestionTypeUsingParameters:request];
    
    NSArray *responseBodyParts=response.bodyParts;
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return probClsVOs;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return probClsVOs;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_GetQuestionTypeResponse class]]) {
        TccSiteServicesSvc_GetQuestionTypeResponse * response = bodyPart;
//        NSLog(@"%@",response.GetQuestionTypeResult);
        NSString * result=response.GetQuestionTypeResult;
        result=[result stringByReplacingOccurrencesOfString:@"<problemClassInfo><problemInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</problemInfo></problemClassInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</problemInfo><problemInfo>" withString:@""];
        result=[result stringByReplacingOccurrencesOfString:@"</pccode><pcname>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</pcname><pccode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"<pccode>" withString:@","];
        result=[result stringByReplacingOccurrencesOfString:@"</pcname>" withString:@","];
//        NSLog(@" after replace%@",result);
        
        NSArray *values=[result componentsSeparatedByString:@","];
        int count=[values count];
        if(count>0){
            probClsVOs =[[NSMutableArray alloc] init];
            for(int i=1;i<count - 1;){
                ProblemClassVO *probClsVO= [[ProblemClassVO alloc] init];
                probClsVO.pccode=[values objectAtIndex:i];
                probClsVO.pcname=[values objectAtIndex:(i+1)]; 
                probClsVO.loginuser=loginuser;
                [probClsVOs addObject:probClsVO];
                i = i+2;
            }
        }
         
    }
    return probClsVOs;

    
}

//项目状态查询
-(int )GetProjectState:(NSString *) userName pwd:(NSString *)pwd projcode :(NSString*)projcode{
    int projStatus=0;
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    TccSiteServicesSoapBindingResponse *response;
    TccSiteServicesSvc_GetProjectState *request=[[TccSiteServicesSvc_GetProjectState alloc] init];
    response=[binding GetProjectStateUsingParameters:request];
    
    NSArray *responseBodyParts=response.bodyParts;
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return projStatus;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return projStatus;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_GetProjectStateResponse class]]) {
        TccSiteServicesSvc_GetProjectStateResponse * response = bodyPart;
//        NSLog(@"%@",response.GetProjectStateResult);
        projStatus=response.GetProjectStateResult;
    }
    return projStatus;

}

//用户同步 
-(NSString *) UserSynchronous:(NSString *) userName pwd:(NSString *)pwd{
    NSString * newPwd;
    TccSiteServicesSoapBinding *binding = [TccSiteServicesSvc TccSiteServicesSoapBinding:serverInfo];
    TccSiteServicesSoapBindingResponse *response;
    TccSiteServicesSvc_UserSynchronous *request=[[TccSiteServicesSvc_UserSynchronous alloc] init];
    response=[binding UserSynchronousUsingParameters:request];
    
    NSArray *responseBodyParts=response.bodyParts;
    id bodyPart;
    @try{
        bodyPart = [responseBodyParts objectAtIndex:0]; // Assuming just 1 part in response which is fine
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:@"Error while trying to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return newPwd;
    }
    
    if ([bodyPart isKindOfClass:[SOAPFault class]]) {
        return newPwd;
    } else if([bodyPart isKindOfClass:[TccSiteServicesSvc_UserSynchronousResponse class]]) {
        TccSiteServicesSvc_UserSynchronousResponse * response = bodyPart;
//        NSLog(@"%@",response.UserSynchronousResult);
        newPwd=[response.UserSynchronousResult stringValue];
    }
    return newPwd;
}


@end
